package com.A21MobileSales;

import java.util.ArrayList; 
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent; 
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle; 
import android.text.Editable;
import android.text.TextWatcher; 
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView; 
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
 
import com.A21.Adapter.ProductListAdapter;  
import com.A21.Adapter.ProductSwipeListAdapter;
import com.A21MobileSales.R;
import com.A21MobileSales.DataBase.DatabaseHelper; 
import com.A21.Object.Global;
import com.A21.Object.MSTProduct; 

public class MTProductSelection extends  Activity {

	public static final int PRODUCT_DETAILS_ACTIVITY = 1; 
	public static final int PRODUCT_SEARCH_ACTIVITY = 2;
	DatabaseHelper ObjDBHelper;
	ArrayList<MSTProduct> lstProducts;
	Button btnDelSearch,btnSearhAdvanced;
	EditText edtSearchProduct;
	ListView lvProduct;
	ProductListAdapter productListAdapter;
	private String CustCode, CName, strId, TrnType;
	String Brand, Model, Made,SearchText;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);  
		setContentView(R.layout.product_selection);
		getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		try {
			ObjDBHelper = new DatabaseHelper(this);
			// lstProducts = ObjDBHelper.GetAllProducts();
			lvProduct = (ListView) findViewById(R.id.OLS_lvOrderLineList);

			if (getIntent().hasExtra("Code")) {
				CustCode = getIntent().getStringExtra("Code");
			}
			if (getIntent().hasExtra("CName")) {
				CName = getIntent().getStringExtra("CName");
			}
			if (getIntent().hasExtra("Id")) {
				strId = getIntent().getStringExtra("Id");
			}
			if (getIntent().hasExtra("TrnType")) {
				TrnType = getIntent().getStringExtra("TrnType");
			}
			Brand="";
			Model="";
			Made="";
			SearchText="";
			lvProduct.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapter, View v,
						int Pos, long Id) {
					// TODO Auto-generated method stub
					try {
						Bundle b = new Bundle();
						b.putString("Code", CustCode);
						b.putString("CName", CName);
						b.putString("Id", "0");
						b.putString("TrnType", TrnType);
						b.putString("Product", lstProducts.get(Pos).Product); 
						Intent _intent = new Intent();
						_intent.putExtras(b);
						_intent.setClass(MTProductSelection.this,
								TrnProductDetails.class);
						
						OrderTab parent = (OrderTab) getParent();
						Global.setA21OrderHeader(parent.GetHeader());
						 startActivityForResult(_intent,PRODUCT_DETAILS_ACTIVITY);  
					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			});
			// lvProduct.setAdapter(new
			// ProductListAdapter(ProductSelection.this, lstProducts));
			edtSearchProduct = (EditText) findViewById(R.id.edtSearchProduct);
			btnDelSearch = (Button) findViewById(R.id.btn_del_search);
			btnDelSearch.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Brand="";
					Model="";
					Made="";
					edtSearchProduct.setText(""); 
				}
			});

			btnSearhAdvanced = (Button) findViewById(R.id.btnSearhAdvanced); 
			btnSearhAdvanced.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) { 
					Bundle b = new Bundle();
					 b.putString("Brand", Brand);
					 b.putString("Model", Model);
					 b.putString("Made",  Made ); 
					Intent _intent = new Intent();
					_intent.putExtras(b);
					_intent.setClass(MTProductSelection.this,
							ProductAdvancedSearch.class); 
					 startActivityForResult(_intent,PRODUCT_SEARCH_ACTIVITY); 
					
				}
			});
			
			edtSearchProduct.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					try { 
						String text = s.toString().toLowerCase(Locale.getDefault());
						SearchText=text;
						productListAdapter.filterByBMM(Brand, Model, Made,SearchText); 
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 
				}
			});
			
			new ProductTask().execute();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void onBackPressed() { 
		OrderTab parent = (OrderTab) getParent();
		parent.getTabHost().setCurrentTab(parent.previous_tab);
	}
	
	@SuppressLint("NewApi")
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case PRODUCT_DETAILS_ACTIVITY:
			if (resultCode == RESULT_OK) { 
				Bundle bundle = data.getExtras();
				String status = bundle.getString("status");
				if (status.equalsIgnoreCase("done")) {
					OrderTab parent = (OrderTab) getParent();
					parent.UpdateDetail();
					Toast.makeText(MTProductSelection.this,
							"Product has been added successfully.",
							Toast.LENGTH_SHORT).show();
				}
			}else if(resultCode == RESULT_CANCELED){
				
				
			}
			break;
		case PRODUCT_SEARCH_ACTIVITY:
			if (resultCode == RESULT_OK) {  
				 Bundle bundle = data.getExtras();
				 String status = bundle.getString("status"); 
				 if (status.equalsIgnoreCase("done")) { 
					 Brand=  bundle.getString("Brand"); 
					 Model=  bundle.getString("Model"); 
					 Made=  bundle.getString("Made"); 
					 productListAdapter.filterByBMM(Brand, Model, Made,SearchText); 
				 }
				 
			}else if(resultCode == RESULT_CANCELED){
				 
			}
			break; 
		}

	}
	
	
	/*@Override
	protected void onResume() {
		super.onResume();
		try {
			lstProducts = ObjDBHelper.GetAllProducts(false);
			lvProduct.setAdapter(new ProductListAdapter(ProductSelection.this,
					lstProducts));
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}*/
	
	 public void bindData() { 
		  productListAdapter = new ProductListAdapter(MTProductSelection.this, lstProducts);
		  productListAdapter.notifyDataSetChanged();
		  lvProduct.setAdapter(productListAdapter);
		}

		private class ProductTask extends AsyncTask<String, Void, Void> {

			private ProgressDialog dialog = new ProgressDialog(MTProductSelection.this);
			String StrMsg = null;

			protected void onPreExecute() {
				this.dialog.setMessage("Loading...");
				this.dialog.setCanceledOnTouchOutside(false);
				this.dialog.show();

			}

			protected Void doInBackground(final String... Params) { 
				try { 
					lstProducts =Global.getMultiTierProduct();
				} catch (Exception ex) {
					ex.printStackTrace();
				}

				return null;
			}

			protected void onPostExecute(final Void unused) {
				if (this.dialog.isShowing()) {
					this.dialog.dismiss();
					this.dialog = null;
				}
				try { 
						bindData(); 
				} catch (Exception ex) {

					ex.printStackTrace();

				}

			}

		}
	
}